package com.ellecity06.cwhiskymall.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ellecity06.common.ktx.load
import com.ellecity06.common.ktx.prefixUnit
import com.ellecity06.common.viewholder.ViewBindingHolder
import com.ellecity06.cwhiskymall.bean.response.AppMenuSettingRespone
import com.ellecity06.cwhiskymall.databinding.ItemHomeMenuTabBinding
import com.ellecity06.cwhiskymall.databinding.ItemNewerGoodBinding
import com.ellecity06.cwhiskymall.databinding.ItemSelfGoodBinding

/**
 * @author ellecity06
 * @time 2024/10/23 13:33
 * @des 商业商品菜单适配器
 */
class HomeMenuAdapter : BaseQuickAdapter<AppMenuSettingRespone.SettingWebLayoutVOS.DetailVO, ViewBindingHolder<ItemHomeMenuTabBinding>>() {

    override fun onBindViewHolder(
        holder: ViewBindingHolder<ItemHomeMenuTabBinding>, position: Int, item: AppMenuSettingRespone.SettingWebLayoutVOS.DetailVO?
    ) {
        holder.binding.tvTitle.text = item?.name
        holder.binding.tvSubTitle.text = "副标题"
        holder.binding.tvTitle.isChecked = item?.checked ?: false
        holder.binding.tvSubTitle.isChecked = item?.checked ?: false
    }

    override fun onBindViewHolder(
        holder: ViewBindingHolder<ItemHomeMenuTabBinding>,
        position: Int,
        item: AppMenuSettingRespone.SettingWebLayoutVOS.DetailVO?,
        payloads: List<Any>
    ) {
        if (payloads != null && payloads.isNotEmpty()) {
            holder.binding.tvTitle.isChecked = item?.checked ?: false
            holder.binding.tvSubTitle.isChecked = item?.checked ?: false
        } else {
            super.onBindViewHolder(holder, position, item, payloads)
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): ViewBindingHolder<ItemHomeMenuTabBinding> {
        return ViewBindingHolder(ItemHomeMenuTabBinding.inflate(LayoutInflater.from(context), parent, false))
    }

    fun switch(item: AppMenuSettingRespone.SettingWebLayoutVOS.DetailVO) {
        if (item.checked) return
        items.forEachIndexed { index, detailVO ->
            if (detailVO.checked) {
                detailVO.checked = false
                notifyItemChanged(index, mutableListOf(false))
                return@forEachIndexed
            }
        }
        item.checked = true
        notifyItemChanged(getItemPosition(item), mutableListOf(true))

    }


}